package com.ruoyi.web.controller.classes;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.classes.domain.vo.ClassUserVo;
import com.ruoyi.common.core.domain.entity.SysUser;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.classes.domain.TClassUser;
import com.ruoyi.classes.service.ITClassUserService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 班级用户连接Controller
 *
 * @author Bandit
 * @date 2022-11-01
 */
@RestController
@RequestMapping("/classes/ClassUser")
public class TClassUserController extends BaseController
{
    @Autowired
    private ITClassUserService tClassUserService;

    /**
     * 查询班级用户连接列表
     */
    @PreAuthorize("@ss.hasPermi('classes:ClassUser:list')")
    @GetMapping("/list")
    public TableDataInfo list(ClassUserVo classUserVo)
    {
        startPage();
        List<ClassUserVo> list = tClassUserService.selectClassUserVoList(classUserVo);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('classes:ClassUser:list')")
    @GetMapping("/unlisted")
    public AjaxResult unlistedList(Long classId)
    {
        List<SysUser> unlistedList = tClassUserService.selectUnlistedListByClassId(classId);
        return AjaxResult.success(unlistedList);
    }

    /**
     * 导出班级用户连接列表
     */
    @PreAuthorize("@ss.hasPermi('classes:ClassUser:export')")
    @Log(title = "班级用户连接", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TClassUser tClassUser)
    {
        List<TClassUser> list = tClassUserService.selectTClassUserList(tClassUser);
        ExcelUtil<TClassUser> util = new ExcelUtil<TClassUser>(TClassUser.class);
        util.exportExcel(response, list, "班级用户连接数据");
    }

    /**
     * 获取班级用户连接详细信息
     */
    @PreAuthorize("@ss.hasPermi('classes:ClassUser:query')")
    @GetMapping
    public AjaxResult getInfo(Long userId, Long classId)
    {
        if (userId == null || classId == null) {
            return error("班级ID或者用户ID为空");
        }
        TClassUser tClassUser = new TClassUser();
        tClassUser.setClassId(classId);
        tClassUser.setUserId(userId);
        return success(tClassUserService.selectClassUserVoByTClassUser(tClassUser));
    }

    /**
     * 新增班级用户连接
     */
    @PreAuthorize("@ss.hasPermi('classes:ClassUser:add')")
    @Log(title = "班级用户连接", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TClassUser tClassUser)
    {
        return toAjax(tClassUserService.insertTClassUser(tClassUser));
    }

    @PreAuthorize("@ss.hasPermi('classes:ClassUser:add')")
    @Log(title = "班级用户连接", businessType = BusinessType.INSERT)
    @PostMapping("/batch")
    public AjaxResult addBatch(@RequestBody List<TClassUser> tClassUserList)
    {
        return toAjax(tClassUserService.insertTClassUserBatch(tClassUserList));
    }

    /**
     * 修改班级用户连接
     */
    @PreAuthorize("@ss.hasPermi('classes:ClassUser:edit')")
    @Log(title = "班级用户连接", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TClassUser tClassUser)
    {
        if (tClassUser.getClassId() == null || tClassUser.getUserId() == null) {
            return error("班级ID或者用户ID为空");
        }
        return toAjax(tClassUserService.updateTClassUser(tClassUser));
    }

    /**
     * 删除班级用户连接
     */
    @PreAuthorize("@ss.hasPermi('classes:ClassUser:remove')")
    @Log(title = "班级用户连接", businessType = BusinessType.DELETE)
	@DeleteMapping
    public AjaxResult remove(@RequestBody List<TClassUser> classUserList)
    {
        return toAjax(tClassUserService.deleteTClassUserByClassClassUserList(classUserList));
    }
}
